package com.eascs.admin.common.utils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * 项目公用方法
 * @author user
 *
 */
public class CommonUtils {
	/**
	 * 组装表单请求参数
	 * @param formClass
	 * @param form
	 * @return
	 */
	public static <T> String getFormUrl(Class<T> formClass, T form) {
		// 组装请求参数
		StringBuffer params = new StringBuffer();
		try {
			Field[] fields = formClass.getDeclaredFields();
			for (Field field : fields) {
				String fieldName = field.getName();
				int modifier = field.getModifiers();
				//只取私有的非类属性
				if (Modifier.PRIVATE == modifier && Modifier.STATIC != modifier) {
					PropertyDescriptor pd = new PropertyDescriptor(fieldName, formClass);
					// 调用get方法，获取属性值
					Method getMethod = pd.getReadMethod();
					Object getValue = getMethod.invoke(form);
					if (null != getValue) {
						params.append("&").append(fieldName).append("=").append(getValue.toString());
					}
				}
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (IntrospectionException e) {
			e.printStackTrace();
		}
		return params.toString();
	}
}
